package com.yowasa.xiuxian.engine.message;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.entity.bot.message.Message;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.util.ContentUtil;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

public abstract class CmdMessageHandler implements BaseMessageHandler {

    @Override
    public boolean match(String message) {
        CMD annotation = this.getClass().getAnnotation(CMD.class);
        String cmd = annotation.name();
        if (StringUtils.isEmpty(cmd)) {
            return false;
        }
//        CmdScopeEnum scope = annotation.scope();
//        switch (scope) {
//            case CHANNEL:
//                if (message.isDirectMessage()) return false;
//                break;
//            case PRIVATE:
//                if (!message.isDirectMessage()) return false;
//                break;
//            case ALL:
//            default:
//                break;
//        }
        String noUserContent = ContentUtil.getNoUserContent(message);
        if (noUserContent.startsWith(cmd)) {
            return true;
        }
        return false;
    }


    public String getParam(SendMessage message) {
        CMD annotation = this.getClass().getAnnotation(CMD.class);
        String cmd = annotation.name();

        JSONObject jsonObject = JSONUtil.parseObj(message.getContent());
        String text = (String) jsonObject.getJSONObject("content").get("text");
        return ContentUtil.getParam(text, cmd);
    }

    public List<String> getAllParam(SendMessage message) {
        CMD annotation = this.getClass().getAnnotation(CMD.class);
        String cmd = annotation.name();
        JSONObject jsonObject = JSONUtil.parseObj(message.getContent());
        String text = (String) jsonObject.getJSONObject("content").get("text");
        return ContentUtil.getAllParam(text, cmd);
    }

}
